<template>
  <div class="header-right">
    <el-button style="color: black;border: none" icon="MessageBox">
      <a target="_blank"  style="text-decoration: none;color:black" href="https://download.dance365.com">下载app</a>
    </el-button>
<!--发布-->
      <el-dropdown class="sty"  size="large">
        <el-button type="primary" icon="Plus">发布</el-button>
        <template #dropdown>
          <el-dropdown-menu style="text-align: center">
            <p style="background-color:#cccccc;width: 160px;text-align: center">发布资源</p>
            <el-dropdown-item >作品</el-dropdown-item>
            <el-dropdown-item :divided="true">教程</el-dropdown-item>
            <el-dropdown-item :divided="true">动态</el-dropdown-item>
            <p style="background-color:#cccccc; width: 160px;text-align: center">发布商品</p>
            <el-dropdown-item>视频课</el-dropdown-item>
            <el-dropdown-item :divided="true">线下课</el-dropdown-item>
            <el-dropdown-item :divided="true">直播课</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
<!--登录后-->
    <el-dropdown v-if="token" style="margin: 7px 0">
      <template #dropdown>
        <el-dropdown-item @click="loOut">点击退出</el-dropdown-item>
      </template>
      <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"/>
    </el-dropdown>
    <el-button v-else type="info" @click="dialogVisible = true">登录</el-button>
    <!-- 登录弹窗 -->
    <el-dialog v-model="dialogVisible" >
      <el-form :model="form">
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input v-model="loginStore.name"/>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="loginStore.password"/>
        </el-form-item>
      </el-form>
      <template #footer>
           <span class="dialog-footer">
              <el-button type="primary" @click="goLogin" >登录</el-button>
           </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import {defineComponent} from "vue";
export default defineComponent({
  name:"UserLogin",
})
</script>

<script setup lang="ts">
import {ref} from "vue";
import {storeToRefs} from "pinia";

// 调用模块
import {useLogin} from "@/store/login"
import {ElMessage} from "element-plus";
import {setToken} from "@/utils/token-utils";
import {useRouter} from "vue-router";
import router from "@/router";

// 调用pinia数据
const loginStore = useLogin();
const {token,name} = storeToRefs(loginStore)
const $router = useRouter();
//控制登录弹窗
let dialogVisible = ref<any>(false)
const formLabelWidth = '140px'

const form = ref<any>({
  delivery: false,
})

// 点击登录
const goLogin= async ()=>{
  // 点击方法之后调用登录请求
  try {
    const token = await loginStore.getInfo()
    setToken(token)
    ElMessage.success('登录成功')
    // 重新跳转页面
    setTimeout(()=>{
      $router.go(0)
    },100)
  }catch (e) {
    ElMessage.error('登录失败')
  }
  dialogVisible.value = false
}
// 点击退出登录
const  loOut =()=>{
  loginStore.reset()
  ElMessage.success('已退出登录')
  $router.push('/index/recommend')
  setTimeout(()=>{
    $router.go(0)
  },10)
}
</script>

<style scoped lang="scss">
.example-showcase .el-dropdown + .el-dropdown {
  margin-left: 15px;
}
.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-danger);
  display: flex;
  align-items: center;
}
.sty{
  margin: 15px 20px;
  border-radius: 50%;
}
.demo-type {
  display: flex;
}
.demo-type > div {
  flex: 1;
  text-align: center;
}

.demo-type > div:not(:last-child) {
  border-right: 1px solid var(--el-border-color);
}
</style>
